import Vue from "vue"
import store from '@/store'
import uniCrazyRouter from "uni-crazy-router";
Vue.use(uniCrazyRouter)

const interceptLogin = uniCrazyRouter.beforeEach(async (to, from, next) => {
	// 判断是否登录，没有登陆就跳转到登录页，并且要去除对登录页的拦截，否则会死循环
	if (!store.state.vuex_token && to.url !== 'pages/login/login'
	     && to.url !== 'pages/index/index'
		 && to.url !== 'pages/login/register') {
		uniCrazyRouter.afterNotNext(() => {
			// 拦截路由，并且跳转去登录页
			uni.showToast({
				title: '您未登录，请先登录'
			})
			setTimeout(() => {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			})
		}, 1000)
		return // 拦截路由，不执行next
	}
	next()
})
